package com.firemoss.magicbus.frameworks.swiz
{
	import com.firemoss.magicbus.messaging.AbstractProducerMediator;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	public class SwizProducerMediator extends AbstractProducerMediator
	{

		[Dispatcher]
		public var swizDispatcher:IEventDispatcher;

		private var _messageSentEvents : Array
		
		override public function subscribeToMessageSentEvents( eventNames : Array ) : void
		{
			var eventName : String 
			
			if ( _messageSentEvents ) 
			{
				for each ( eventName in _messageSentEvents )
				{ 
					// Handle both bubbled UI and centrally dispatched events
					swizDispatcher.removeEventListener( eventName, messageSent )
				}
			}
			
			_messageSentEvents = eventNames
		
			if ( _messageSentEvents )
			{
				for each ( eventName in _messageSentEvents )
				{
					swizDispatcher.addEventListener( eventName, messageSent, false, 0, true )
				}
			}
		}
		
		override public function dispatch(event:Object):void
		{
			swizDispatcher.dispatchEvent( Event( event ) )
		}
		
		public function SwizProducerMediator()
		{
			super();
		}
		
	}
}